local sk__danjing = fk.CreateSkill {

  name = "sk__danjing",

  tags = {},

}



sk__danjing:addEffect(fk.Damaged, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__danjing.name) then
      if target == player and #player.room.alive_players > 0 then
        event:setCostData(self, data.damage)
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(room:getOtherPlayers(player), Util.IdMapper)
    local prompt
    prompt = 1
    local to = room:askForChoosePlayers(player, targets, 1, 1,
      "#sk__danjing" .. prompt .. "-choose:::" .. event:getCostData(self), sk__danjing.name, true, false)
    if #to > 0 then
      event:setCostData(self, { to[1], event:getCostData(self) })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self)[1])
    local n = event:getCostData(self)[2]
    room:damage {
      from = player,
      to = to,
      damage = data.damage,
      damageType = data.damageType,
      skillName = sk__danjing.name,
    }
  end,
})
sk__danjing:addEffect(fk.HpLost, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__danjing.name) then
      if target == player and #player.room.alive_players > 0 then
        event:setCostData(self, data.num)
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(room:getOtherPlayers(player), Util.IdMapper)
    local prompt
    prompt = 2
    local to = room:askForChoosePlayers(player, targets, 1, 1,
      "#sk__danjing" .. prompt .. "-choose:::" .. event:getCostData(self), sk__danjing.name, true, false)
    if #to > 0 then
      event:setCostData(self, { to[1], event:getCostData(self) })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self)[1])
    local n = event:getCostData(self)[2]
    room:loseHp(to, data.num, sk__danjing.name)
  end,
})
sk__danjing:addEffect(fk.MaxHpChanged, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__danjing.name) then
      if target == player and #player.room.alive_players > 0 then
        event:setCostData(self, -data.num)
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(room:getOtherPlayers(player), Util.IdMapper)
    local prompt
    prompt = 3
    local to = room:askForChoosePlayers(player, targets, 1, 1,
      "#sk__danjing" .. prompt .. "-choose:::" .. event:getCostData(self), sk__danjing.name, true, false)
    if #to > 0 then
      event:setCostData(self, { to[1], event:getCostData(self) })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self)[1])
    local n = event:getCostData(self)[2]
    room:changeMaxHp(to, data.num)
  end,
})
sk__danjing:addEffect(fk.AfterCardsMove, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__danjing.name) then
      local n = 0
      for _, move in ipairs(data) do
        if move.from and move.from == player and move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              n = n + 1
            end
          end
        end
      end
      if n > 0 and table.find(player.room:getOtherPlayers(player), function(p)
            return not p:isNude()
          end) then
        event:setCostData(self, n)
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(room:getOtherPlayers(player), Util.IdMapper)
    local prompt
    targets = table.map(table.filter(room:getOtherPlayers(player), function(p)
      return not p:isNude()
    end), Util.IdMapper)
    prompt = 4
    local to = room:askForChoosePlayers(player, targets, 1, 1,
      "#sk__danjing" .. prompt .. "-choose:::" .. event:getCostData(self), sk__danjing.name, true, false)
    if #to > 0 then
      event:setCostData(self, { to[1], event:getCostData(self) })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self)[1])
    local n = event:getCostData(self)[2]
    room:askForDiscard(to, n, n, true, sk__danjing.name, false)
  end,
})

return sk__danjing
